package com.woniuxy.comsumer.listen;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
public class RabbitListener {
    @Autowired
    private CachingConnectionFactory cachingConnectionFactory;

    /**
     * 消息监听器容器工厂
     * @return
     */
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(){
        SimpleRabbitListenerContainerFactory containerFactory = new SimpleRabbitListenerContainerFactory();
        //连接工厂
        containerFactory.setConnectionFactory(cachingConnectionFactory);
        // ACK模式
        containerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        // 预取数量
        containerFactory.setPrefetchCount(1);
        //设置使用json反序列化
        containerFactory.setMessageConverter(new Jackson2JsonMessageConverter());
        //并发消费者数量
        containerFactory.setConcurrentConsumers(4);
        //最大的并发消费者数量
        containerFactory.setMaxConcurrentConsumers(10);
        return containerFactory;
    }
}
